import {
	WeaTools
} from 'ecCom';
import React, {
	Component
} from 'react';
import {
	WeaDialog,
	WeaLocaleProvider
} from 'ecCom';
import {
	Button
} from 'antd';
import ReactDOM from 'react-dom';
const getLabel = WeaLocaleProvider.getLabel;

const isWeakPassword = (params = {}) => WeaTools.callApi('/api/hrm/password/isWeakPassword', 'POST', params);

export const checkWeakPass = (password,onOk,onCancel) => {
	isWeakPassword({
		password
	}).then(datas => {
		const {
			weakPasswordDisable,
			isWeakPassword,
			defaultPasswordEnable
		} = datas;

		if (isWeakPassword) {
			const div = document.createElement("div");
    		document.body.appendChild(div);

			ReactDOM.render(<PassConfirm ecId={`${this && this.props && this.props.ecId || ''}_PassConfirm@lr0wli`} 
				weakPasswordDisable={weakPasswordDisable}
				onOk={onOk}
				onCancel={onCancel}
			/>,div);
		} else {
			onOk();
		}
	}).catch(() => {
		onOk(); 
	})
}

class PassConfirm extends Component{
	constructor(props){
		super(props);

		this.state = {
			visible:false
		}
	}

	componentDidMount() {
		this.setState({visible:true});
	}

	handleOk = () => {
		const {weakPasswordDisable,onOk}= this.props;

		if (weakPasswordDisable === "0") {
			onOk && onOk();
			this.setState({visible:false});
		}else{
			this.handleCancel();
		}
	}

	handleCancel = () => {
		const {onCancel} = this.props;
		this.setState({visible:false});
		onCancel && onCancel();
	}

	getButtons = () => {
		const {weakPasswordDisable}= this.props;

		const btns =[
			<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@49lufi`} onClick={this.handleOk} type="primary">{getLabel(33703, '确定')}</Button>,
		]

		if (weakPasswordDisable === "0") {
			btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@1z4xow`} onClick={this.handleCancel}>{getLabel(32694, '取消')}</Button>)
		}

		return btns;
	}

	render() {
		const {
			visible
		} = this.state, {
			weakPasswordDisable
		} = this.props;

		const pstyle = {
			display: "table-cell",
			verticalAlign: "middle",
			padding: "0 20px"
		}

		if (!visible) {
			return null;
		}

		return (
			<WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@ynj04k`} 
                title={getLabel(131329, '信息确认')}
                icon="icon-coms02-Warning"
                iconBgcolor="#ff9900"
                visible={visible}
                style={{width:380,height:100}}
                onCancel={this.handleCancel}
                buttons={this.getButtons()}
            >
            	<div style={{height:"100%",display: "table",margin: "0 auto"}}>
	              <p style={pstyle}>
	                  {
	                    (weakPasswordDisable === "0") ? getLabel('515419','该密码比较容易被猜到，确定保存吗？')  : getLabel('515420','该密码比较容易被猜到，为了您的系统安全，请重新修改密码。') 
	                  }
	              </p>
            	</div>
            </WeaDialog>
		);
	}
}

